// pages/my/stockManage/stockManage.js
var app = getApp()
Page({
  data: {
    brandList:[],
    partss:[],
    vehicles:[],
    vehicleStock:'',
    partsStock:'',
    brand:'',
    brandId:'',
    brandPick:'',
    brandPickId:'',
    stockIndex:0,
    pickerView:false,
  },
  onLoad: function(options) {
    app.getMemInfo().then(res=>{
      const agentId = res.data.agentId
      this.setData({
        agentId
      })
      this.getBrands(agentId)
    })
  },
  stock(){
  	wx.navigateTo({
      url: 'stockManage?type=0',
    })
  },
  goods(){
  	wx.navigateTo({
      url: 'stockManage?type=1',
    })
  },
  price(){
	wx.navigateTo({
      url: 'stockManage?type=2',
    })
  },
  //根据代理商ID获取下属品牌列表
  getBrands(agentId){
    let data = {
      agentId
    }
    app.request('wx_app/banner/getByAgentId',data,'GET').then(res=>{
      console.log(res)
      let brandList = []
      brandList.push({
        name:'全部',
        brand_id:''
      })
      res.data.forEach(item=>{
        brandList.push(item)
      })
      
      this.setData({
        brandList,
        brand:brandList[0].name,
        brandId:brandList[0].brand_id,
        brandPick:'全部',
      })
      this.getBrandStock(brandList[0].brand_id)
    })
  },
  bindPickerChange(e) {
    console.log(e.detail.value)
    this.setData({
      brandPick:this.data.brandList[e.detail.value[0]].name,
      brandPickId:this.data.brandList[e.detail.value[0]].brand_id
    })

  },
  closePicker(){
    this.setData({
      pickerView:false
    })
  },
  openPicker(e){
    this.setData({
      pickerView:true,
    })
  },
  confirmPicker(){
    this.setData({
      pickerView:false,
      brand:this.data.brandPick,
      brandId:this.data.brandPickId
    })
    this.getBrandStock(this.data.brandPickId)
  },
  getBrandStock(brandId){
    let data = {
      agentId:this.data.agentId,
      brandId
    }
    console.log(data)
    app.request('wx_app/agent/stock/inventoryData',data,'GET').then(res=>{
      console.log(res)
      let vehicles = []
      let partss = []
      res.data.vehicles.forEach(item=>{
        vehicles.push(item)
      })
      res.data.partss.forEach(item=>{
        partss.push(item)
      })
      this.setData({
        vehicleStock:res.data.vehicle.stock,
        partsStock:res.data.parts.stock,
        oldStock:res.data.oldSku.stock,
        vehicles,
        partss
      })
    })
  },
  getParts(e){
    let i = e.currentTarget.dataset.i
    this.setData({
      stockIndex:i
    })
  },
  getVehicles(e){
    let i = e.currentTarget.dataset.i
    this.setData({
      stockIndex:i
    })
  },
})